In this graph we are plotting the MSCI data taken from here: https://finance.yahoo.com/quote/MSCI/history?period1=1527807600&period2=1559343600&interval=1d&filter=history&frequency=1d
# Create a times series plot of Open against Date
msci %>%
plot_ly(x = ~Date, y = ~Open) %>%
add_lines()
# Create a scatterplot with Date on the x-axis and Volume on the y-axis
msci %>%
plot_ly(x = ~Date, y = ~Volume) %>%
add_markers()
Happiness Report
# Create a coded scatterplot of happiness vs. life.expectancy for 2018
happycountries %>%
filter(Year == 2018) %>%
plot_ly(x = ~life.expectancy, y = ~happiness) %>%
add_markers(color = ~region, size = ~log.gdp)
# Plotting symbols and polishing the graphic with tooltip and labels
happycountries %>%
filter(Year == 2018) %>%
plot_ly(x = ~life.expectancy, y = ~happiness,
hoverinfo = "text",
text = ~paste("Country: ", country,
"<br> Income: ", income,
"<br> Happiness: ", round(happiness, 2),
"<br> Life Expectancy: ", round(life.expectancy, 2),
"<br> GDP Per Capita: ", round(gdp.per.capita, 2))) %>%
add_markers(symbol = ~income,
symbols = c("circle-open", "square-open", "star-open", "x-thin-open")) %>%
layout(xaxis = list(title = "Life Expectancy"),
yaxis = list(title = "National Happiness Score"))
Import new data set
# Create a scatter plot of house_price over time by state
us_economy %>%
filter(year == 2017) %>%
plot_ly(x = ~gdp, y = ~house_price) %>%
add_markers(size = ~population,
color = ~region,
marker = list(sizemode = "diameter"))
# Create a line chart of house_price over time by state
us_economy %>%
filter(year >= 2000) %>%
group_by(state) %>%
plot_ly(x = ~year, y = ~house_price) %>%
add_lines()
Adding Animation
# Create an animated bubble chart of house_price against gdp
us_economy %>%
plot_ly(
x = ~gdp, y = ~house_price,
hoverinfo = "text", text = ~state
) %>%
add_markers(
size = ~population, color = ~region,
frame = ~year, ids = ~state,
marker = list(sizemode = "diameter", sizeref = 2) # size ref changes the size of the bubble
) %>%
layout(
xaxis = list(title = "Real GDP (millions USD)", type = "log"),
yaxis = list(title = "Housing price index")
)
# Animate a bubble chart of house_price against gdp over region
ani <- us_economy %>%
filter(year == 2017) %>%
plot_ly(x = ~gdp, y = ~house_price) %>%
add_markers(size = ~population, color = ~region,
frame = ~region, ids = ~state,
marker = list(sizemode = "diameter"))
Animation Attributes https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
# ani is the plot created above
ani %>%
animation_opts(
frame = 2000,
transition = 300, #this is how long the frame takes
easing = "elastic"
) %>%
animation_slider(
currentvalue = list(
prefix = NULL, # this get rid of the slider
font = list(color = "red") # changes the color on the slider
)
) %>%
layout(
xaxis = list(title = "Real GDP (millions USD)"),
yaxis = list(title = "Housing price index")
)
Adding background text
# Add the year as background text and remove the slider
us_economy %>%
plot_ly(x = ~gdp, y = ~house_price, hoverinfo = "text", text = ~state) %>%
add_text(x = 200000, y = 450, text = ~year, frame = ~year, # this is where the code is
textfont = list(color = toRGB("gray80"), size = 150)) %>%
add_markers(size = ~population, color = ~region,
frame = ~year, ids = ~state,
marker = list(sizemode = "diameter", sizeref = 3)) %>%
layout(xaxis = list(title = "Real GDP (millions USD)", type = "log"),
yaxis = list(title = "Housing price index")) %>%
animation_slider(hide = TRUE)
Layering Plotting the baseline - keeping dots in the background You can use different data sets for different layers in plotly, just like you can in ggplot2. This is a useful strategy when you want to display a baseline time point (or group) in an animation. Here is an animated scatterplot of housing price index against real GDP, keeping the 1997 data points in the background.
# extract the 1997 data
us1997 <- us_economy %>%
filter(year == 1997)
# create an animated scatterplot with baseline from 1997
us_economy %>%
plot_ly(x = ~gdp, y = ~house_price) %>%
add_markers(data = us1997, marker = list(color = toRGB("gray60"), opacity = 0.5)) %>%
add_markers(frame = ~year, ids = ~state, data = us_economy, showlegend = FALSE, alpha = 0.5) %>%
layout(xaxis = list(type = "log"))